package com.message.queue.rabbit.confirms;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by zhangbo on 2020/7/17.
 */
@Configuration
public class MessageListenerConfig {


    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Autowired
    private MyAckReceiver myAckReceiver;

    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer() {
        SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer(connectionFactory);
        simpleMessageListenerContainer.setConcurrentConsumers(1);
        simpleMessageListenerContainer.setMaxConcurrentConsumers(1);
        /*RabbitMQ默认是自动确认，这里改为手动确认消息*/
        simpleMessageListenerContainer.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        /*设置一个队列*/
        simpleMessageListenerContainer.setQueueNames("confirmsQueue");
        //同时设置多个队列：队列必须已创建
//        simpleMessageListenerContainer.setQueueNames("confirmsQueue1","confirmsQueue2","confirmsQueue3");

        //另一种设置队列的方法,如果使用这种情况,那么要设置多个,就使用addQueues
        //container.setQueues(new Queue("TestDirectQueue",true));
        //container.addQueues(new Queue("TestDirectQueue2",true));
        //container.addQueues(new Queue("TestDirectQueue3",true));

        /*设置处理器*/
        simpleMessageListenerContainer.setMessageListener(myAckReceiver);
        return simpleMessageListenerContainer;
    }

}
